import { message } from 'ant-design-vue'
import { useI18n } from '@/locales/useI18n'
import { Status, Result } from '@/http/types.d'

const { t } = useI18n()

type MessageInfoType = {
  error: string
  success?: string
}

export async function useResponse(
  result: Result,
  messageInfo?: MessageInfoType,
  callback?: Function
): Promise<any> {
  const { error, success } = messageInfo || {
    error: t('system.api.apiRequestFailed')
  }

  if (result.status === Status.SUCCESS) {
    callback && callback(result)
    success && message.success(success)
    return Promise.resolve(result)
  }
  message.error(error)
  return Promise.reject()
}